//
//  DesignCenterCollectionTableCell.m
//  ForestPack
//
//  Created by jimmy on 2018/7/5.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "DesignCenterCollectionTableCell.h"
#import "FPDesignCollectionViewCell.h"
#import "FPCommodityDetailVC.h"
#import "RecommendProductModel.h"

@interface DesignCenterCollectionTableCell()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, assign) CGFloat  cellHeight;
@property (nonatomic, strong) NSIndexPath * indexPath;
@property (nonatomic, strong) UICollectionView * collectionView;

@end


@implementation DesignCenterCollectionTableCell


+ (DesignCenterCollectionTableCell *)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath andCellHeight:(CGFloat)height
{
    static NSString *cellID = @"DesignCollectionCell";
    DesignCenterCollectionTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[DesignCenterCollectionTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.indexPath = indexPath;
        cell.cellHeight = height;
        [cell setCollectionViewUIWithIndexPath:indexPath];
    }
    return cell;
}


- (void)setCollectionViewUIWithIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.itemSize = CGSizeMake(self.cellHeight - 40,self.cellHeight-30);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 0);
    
    //设置CollectionView的属性
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.cellHeight) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.scrollsToTop = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    [self addSubview:self.collectionView];
    
    [self.collectionView registerClass:[FPDesignCollectionViewCell class] forCellWithReuseIdentifier:@"designCollectionViewCell"];
    
}

- (void)setDataArry:(NSMutableArray *)dataArry {
    _dataArry = dataArry;
    [self.collectionView reloadData];
}

#pragma mark  设置CollectionView每组所包含的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArry.count;
    
}

#pragma mark  设置CollectionCell的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FPDesignCollectionViewCell *designCollectionCell = [FPDesignCollectionViewCell cellWithCollectionView:collectionView andIndexPath:indexPath];
    RecommendProductModel *model = _dataArry[indexPath.row];
    [designCollectionCell initwithmodel:model];
    return designCollectionCell;
}

#pragma mark  点击CollectionView触发事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"---------------------");
    if (_commodityClick) {
        _commodityClick(indexPath.row);
    }
    
}

@end



@interface YourDesignCenterCollectionTableCell()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, assign) CGFloat  cellHeight;
@property (nonatomic, strong) NSIndexPath * indexPath;
@property (nonatomic, strong) UICollectionView * collectionView;

@end

@implementation YourDesignCenterCollectionTableCell

+ (YourDesignCenterCollectionTableCell *)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath andCellHeight:(CGFloat)height
{
    static NSString *cellID = @"YourDesignCenterCell";
    YourDesignCenterCollectionTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[YourDesignCenterCollectionTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.indexPath = indexPath;
        cell.cellHeight = height;
        [cell setCollectionViewUIWithIndexPath:indexPath];
    }
    return cell;
}

- (void)setCollectionViewUIWithIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.itemSize = CGSizeMake(self.cellHeight-70,self.cellHeight-30);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 0);
    
    //设置CollectionView的属性
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.cellHeight) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.scrollsToTop = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    [self addSubview:self.collectionView];
    
    [self.collectionView registerClass:[FPYourDesignCollectionViewCell class] forCellWithReuseIdentifier:@"yourDesignCollectionViewCell"];
    
}

- (void)setDataArray:(NSMutableArray *)dataArray {
    _dataArray = dataArray;
    [self.collectionView reloadData];
}

#pragma mark  设置CollectionView每组所包含的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}

#pragma mark  设置CollectionCell的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FPYourDesignCollectionViewCell *yourDesignCell = [FPYourDesignCollectionViewCell cellWithCollectionView:collectionView andIndexPath:indexPath];
    YourDesignerModel *model = [_dataArray objectAtIndex:indexPath.row];
    [yourDesignCell initwithmodel:model];
    return yourDesignCell;
}

#pragma mark  点击CollectionView触发事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_contactedClick) {
        _contactedClick(indexPath.row);
    }
}
@end
